package test_new;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class FileCopyStrings {

	public static void main(String[] args) throws IOException {
		if (args.length == 2) {
			copyFileReplace(args[0], args[1], new HashMap<String, String>());			
		} else if (args.length == 3) {
			copyFileReplace(args[0], args[1], createVocabulary(args[2]));
		} else {
			System.out.println("Гони словарь!!!");
		}
		
	}

	private static void copyFileReplace(String nameIn, String nameOut, Map<String, String> vocabResult) throws IOException {
		BufferedReader inFile = new BufferedReader(new FileReader(nameIn));
		BufferedWriter outFile = new BufferedWriter(new FileWriter(nameOut));
		try {
			String stringReplace;
			while ((stringReplace = inFile.readLine()) != null)	{
				for (String key: vocabResult.keySet()) {
					stringReplace = stringReplace.replaceAll(key, vocabResult.get(key));
				}
				outFile.write(stringReplace);
				outFile.newLine();
			}
		} finally {
			inFile.close();
			outFile.close();
		}			
	}	

	static Map<String, String> createVocabulary(String vocabList) throws IOException {
		Map<String, String> vocabResult = new HashMap<String, String>();
		Scanner vocabFileLineRead = new Scanner(new FileReader(vocabList));
		try {
			while (vocabFileLineRead.hasNextLine()) {
				String[] vocabResultArrey = vocabFileLineRead.nextLine().split("->");
				vocabResult.put(vocabResultArrey[0], vocabResultArrey[1]);
			}
		} finally {
			vocabFileLineRead.close();
		}
		return vocabResult;
	}

}